package com.block.api.controller.externalapi;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.db.entity.RiskResultInfo;
import com.block.risk.model.ReportUtil;
import com.block.risk.service.IRiskResultInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 风控  用户授权书 相关接口
 * 给风控提供商 使用
 */
@Controller
@RequestMapping("page/risk")
public class RiskPageController {

    @Autowired
    IRiskResultInfoService riskResultInfoService;

    /**
     * 授权书
     *
     */
    @RequestMapping(value = "/approve/{recordId}/{secretKey}")
    public String checkUser(@PathVariable("recordId") String recordId,
                            @PathVariable("secretKey") String secretKey,
                            ModelMap modelMap){

        RiskResultInfo resultInfo = riskResultInfoService.getById(recordId);
        if (ObjectUtil.isEmpty(resultInfo) ){
            modelMap.put("message", "查询不到数据");
            return "error";
        }
        if (!StrUtil.equals(secretKey, SecureUtil.md5(resultInfo.getMobile()).toLowerCase())){
            modelMap.put("message", "无访问权限");
            return "error";
        }


        modelMap.put("realName", resultInfo.getUsername());
        modelMap.put("mobileNo", resultInfo.getMobile());
        modelMap.put("idNo", resultInfo.getIdcard());
        modelMap.put("dateTime", LocalDate.now().toString());

        return "approve";
    }

    /**
     * 风控报告
     * 给机构使用
     * 查看风控报告
     */
    @RequestMapping(value = "/report/{recordId}/{secretKey}")
    public String report(@PathVariable("recordId") String recordId,
                            @PathVariable("secretKey") String secretKey,
                            ModelMap modelMap){

        try {
            RiskResultInfo resultInfo = riskResultInfoService.getById(recordId);
            if (ObjectUtil.isEmpty(resultInfo)){
                modelMap.put("message", "查询不到数据");
                return "error";
            }

            String  signStr = SecureUtil.md5(StrUtil.format("{}&{}&{}", resultInfo.getId(), resultInfo.getDataId(), resultInfo.getDataType()));
            if (!StrUtil.equalsIgnoreCase(signStr, secretKey)){
                modelMap.put("message", "无访问权限");
                return "error";
            }

            modelMap.put("orderNo", String.valueOf(resultInfo.getId()));
            modelMap.put("username", DesensitizedUtil.chineseName(resultInfo.getUsername()));
            modelMap.put("mobile", DesensitizedUtil.mobilePhone(resultInfo.getMobile()));

            String resultInfoResult = resultInfo.getResult();
            JSONObject resultInfoResultObject = JSON.parseObject(resultInfoResult);
            String code = resultInfoResultObject.getString("code");
            String desc = resultInfoResultObject.getString("desc");

            if (!StrUtil.equals(code, "0")){
                modelMap.put("message", desc);
                return "error";
            }

            JSONObject resultDetail = resultInfoResultObject.getJSONObject("result_detail");

            List<Map<String, String>> applyReportDetailList = ReportUtil.getApplyReportDetailMap(resultDetail);
            modelMap.put("applyReportDetailList", applyReportDetailList);
            List<Map<String, String>> behaviorReportDetailList = ReportUtil.getBehaviorReportDetailMap(resultDetail);
            modelMap.put("behaviorReportDetailList", behaviorReportDetailList);
            List<Map<String, String>> currentReportDetailList = ReportUtil.getCurrentReportDetailMap(resultDetail);
            modelMap.put("currentReportDetailList", currentReportDetailList);

            return "report";
        }catch (Exception exception){
            exception.printStackTrace();
            modelMap.put("message", "查询异常");
            return "error";
        }
    }
}